package com.zhx.common.user.service.adapter;

import cn.hutool.core.bean.BeanUtil;
import com.zhx.common.chat.domain.dto.ChatMessageMarkDTO;
import com.zhx.common.chat.domain.dto.ChatMsgRecallDTO;
import com.zhx.common.chat.domain.vo.response.ChatMemberStatisticResp;
import com.zhx.common.chat.domain.vo.response.ChatMessageResp;
import com.zhx.common.chat.service.ChatService;
import com.zhx.common.user.domain.entity.User;
import com.zhx.common.user.domain.enums.ChatActiveStatusEnum;
import com.zhx.common.user.domain.vo.resp.ws.WSMsgMark;
import com.zhx.common.user.domain.vo.resp.ws.WSMsgRecall;
import com.zhx.common.websocket.domain.enums.WSRespTypeEnum;
import com.zhx.common.websocket.domain.vo.resp.ChatMemberResp;
import com.zhx.common.websocket.domain.vo.resp.WSBaseResp;
import com.zhx.common.websocket.domain.vo.resp.WSFriendApply;
import com.zhx.common.websocket.domain.vo.resp.WSOnlineOfflineNotify;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;

@Component
public class WSAdapter {

    @Resource
    private ChatService chatService;

    public static WSBaseResp<ChatMessageResp> buildMsgSend(ChatMessageResp chatMessageResp){
        WSBaseResp<ChatMessageResp> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.MESSAGE.getType());
        wsBaseResp.setData(chatMessageResp);
        return wsBaseResp;
    }

    public static WSBaseResp<WSMsgRecall> buildMsgRecall(ChatMsgRecallDTO recallDTO) {
        WSBaseResp<WSMsgRecall> wsBaseRespWSBaseResp = new WSBaseResp<>();
        wsBaseRespWSBaseResp.setType(WSRespTypeEnum.RECALL.getType());
        WSMsgRecall wsMsgRecall = new WSMsgRecall();
        BeanUtil.copyProperties(recallDTO,wsMsgRecall);
        wsBaseRespWSBaseResp.setData(wsMsgRecall);
        return wsBaseRespWSBaseResp;
    }

    public static WSBaseResp<WSMsgMark> buildMsgMarkSend(ChatMessageMarkDTO dto, Integer markCount) {
        WSMsgMark.WSMsgMarkItem wsMsgMarkItem = new WSMsgMark.WSMsgMarkItem();
        BeanUtil.copyProperties(dto,wsMsgMarkItem);
        wsMsgMarkItem.setMarkCount(markCount);
        WSBaseResp<WSMsgMark> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.MARK.getType());
        WSMsgMark mark = new WSMsgMark();
        mark.setMarkList(Collections.singletonList(wsMsgMarkItem));
        wsBaseResp.setData(mark);
        return wsBaseResp;
    }

    public static WSBaseResp<?> buildApplySend(WSFriendApply wsFriendApply) {
        WSBaseResp<WSFriendApply> wsBaseResp = new WSBaseResp<>();
        wsBaseResp.setType(WSRespTypeEnum.APPLY.getType());
        wsBaseResp.setData(wsFriendApply);
        return wsBaseResp;
    }

    public static WSBaseResp<?> buildScanSuccessResp() {
        WSBaseResp wsBaseResp = new WSBaseResp();
        wsBaseResp.setType(WSRespTypeEnum.LOGIN_SCAN_SUCCESS.getType());
        return wsBaseResp;
    }

    /**
     * 构建离线通知响应对象。
     *
     * @param user 用户信息，用于构建离线信息。
     * @return WSBaseResp<WSOnlineOfflineNotify> 包含离线通知数据的响应对象。
     */
    public WSBaseResp<WSOnlineOfflineNotify> buildOfflineNotifyResp(User user) {
        // 创建基础响应对象
        WSBaseResp<WSOnlineOfflineNotify> wsBaseResp = new WSBaseResp<>();
        // 设置响应类型为在线离线通知
        wsBaseResp.setType(WSRespTypeEnum.ONLINE_OFFLINE_NOTIFY.getType());

        // 创建在线离线通知对象，并设置离线信息列表
        WSOnlineOfflineNotify onlineOfflineNotify = new WSOnlineOfflineNotify();
        onlineOfflineNotify.setChangeList(Collections.singletonList(buildOfflineInfo(user)));

        // 组装数量信息到在线离线通知对象中
        assembleNum(onlineOfflineNotify);

        // 设置数据到响应对象中
        wsBaseResp.setData(onlineOfflineNotify);

        return wsBaseResp;
    }

    private static ChatMemberResp buildOfflineInfo(User user) {
        ChatMemberResp info = new ChatMemberResp();
        BeanUtil.copyProperties(user, info);
        info.setUid(user.getId());
        info.setActiveStatus(ChatActiveStatusEnum.OFFLINE.getStatus());
        info.setLastOptTime(user.getLastOptTime());
        return info;
    }

    private void assembleNum(WSOnlineOfflineNotify onlineOfflineNotify) {
        ChatMemberStatisticResp memberStatistic = chatService.getMemberStatistic();
        onlineOfflineNotify.setOnlineNum(memberStatistic.getOnlineNum());
    }

    /**
     * 构建在线状态通知响应
     *
     * @param user 用户信息
     * @return WSBaseResp<?> 包含在线或离线通知数据的响应对象
     */
    public WSBaseResp<?> buildOnlineNotifyResp(User user) {
        // 创建一个空的响应对象
        WSBaseResp<WSOnlineOfflineNotify> wsBaseResp = new WSBaseResp<>();
        // 设置响应类型为在线或离线通知
        wsBaseResp.setType(WSRespTypeEnum.ONLINE_OFFLINE_NOTIFY.getType());
        // 初始化在线或离线通知内容
        WSOnlineOfflineNotify onlineOfflineNotify = new WSOnlineOfflineNotify();
        // 构建用户在线信息列表，并设置到通知内容中
        onlineOfflineNotify.setChangeList(Collections.singletonList(buildOnlineInfo(user)));
        // 组装数量信息到通知内容中
        assembleNum(onlineOfflineNotify);
        // 设置通知内容到响应对象中
        wsBaseResp.setData(onlineOfflineNotify);
        return wsBaseResp;
    }


    private static ChatMemberResp buildOnlineInfo(User user) {
        ChatMemberResp info = new ChatMemberResp();
        BeanUtil.copyProperties(user, info);
        info.setUid(user.getId());
        info.setActiveStatus(ChatActiveStatusEnum.ONLINE.getStatus());
        info.setLastOptTime(user.getLastOptTime());
        return info;
    }
}
